home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / ADA / GNAT / !gcc / adainc / 6 / adb / s-wchstw < prev    next >
Text File  |  1996-02-12  |  6KB  |  149 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                       S Y S T E M . W C H _ S T W                        --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.10 $                             --
  10. --                                                                          --
  11. --   Copyright (C) 1992,1993,1994,1995,1996 Free Software Foundation, Inc.  --
  12. --                                                                          --
  13. -- GNAT is free software;  you can  redistribute it  and/or modify it under --
  14. -- terms of the  GNU General Public License as published  by the Free Soft- --
  15. -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
  16. -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
  17. -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
  18. -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
  19. -- for  more details.  You should have  received  a copy of the GNU General --
  20. -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
  21. -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
  22. -- MA 02111-1307, USA.                                                      --
  23. --                                                                          --
  24. -- As a special exception,  if other files  instantiate  generics from this --
  25. -- unit, or you link  this unit with other files  to produce an executable, --
  26. -- this  unit  does not  by itself cause  the resulting  executable  to  be --
  27. -- covered  by the  GNU  General  Public  License.  This exception does not --
  28. -- however invalidate  any other reasons why  the executable file  might be --
  29. -- covered by the  GNU Public License.                                      --
  30. --                                                                          --
  31. -- GNAT was originally developed  by the GNAT team at  New York University. --
  32. -- It is now maintained by Ada Core Technologies Inc (http://www.gnat.com). --
  33. --                                                                          --
  34. ------------------------------------------------------------------------------
  35.  
  36. with System.WCh_Con; use System.WCh_Con;
  37. with System.WCh_JIS; use System.WCh_JIS;
  38.  
  39. package body System.WCh_StW is
  40.  
  41.    ---------------------------
  42.    -- String_To_Wide_String --
  43.    ---------------------------
  44.  
  45.    function String_To_Wide_String
  46.      (S    : String;
  47.       EM   : WC_Encoding_Method)
  48.       return Wide_String
  49.    is
  50.       R  : Wide_String (1 .. S'Length);
  51.       RP : Natural;
  52.       SP : Natural;
  53.  
  54.       Use_Hex : constant Boolean := (EM = WCEM_Hex or else EM = WCEM_None);
  55.  
  56.       function Get_Hex (C : Character) return Natural;
  57.       --  Converts character from hex digit to value in range 0-15. The
  58.       --  input must be in 0-9, A-F, or a-f, and no check is needed.
  59.  
  60.       function Get_Hex (C : Character) return Natural is
  61.       begin
  62.          if C in '0' .. '9' then
  63.             return Character'Pos (C) - Character'Pos ('0');
  64.          elsif C in 'A' .. 'F' then
  65.             return Character'Pos (C) - Character'Pos ('A') + 10;
  66.          else
  67.             return Character'Pos (C) - Character'Pos ('a') + 10;
  68.          end if;
  69.       end Get_Hex;
  70.  
  71.    --  Start of processing for String_To_Wide_String
  72.  
  73.    begin
  74.       SP := S'First;
  75.       RP := 0;
  76.  
  77.       while SP <= S'Last loop
  78.          RP := RP + 1;
  79.  
  80.          if (S (SP) = Ascii.ESC and then Use_Hex) then
  81.             R (RP) := Wide_Character'Val (
  82.                Get_Hex (S (SP + 4)) + 16 *
  83.                  (Get_Hex (S (SP + 3)) + 16 *
  84.                    (Get_Hex (S (SP + 2)) + 16 *
  85.                      (Get_Hex (S (SP + 1))))));
  86.             SP := SP + 5;
  87.  
  88.          --  One-byte ASCII character
  89.  
  90.          elsif S (SP) <= Ascii.DEL or else Use_Hex then
  91.             R (RP) := Wide_Character'Val (Character'Pos (S (SP)));
  92.             SP := SP + 1;
  93.  
  94.             --  Upper bit shift, internal code = external code
  95.  
  96.          elsif EM = WCEM_Upper then
  97.             R (RP) := Wide_Character'Val (
  98.                         Character'Pos (S (SP)) * 256 +
  99.                         Character'Pos (S (SP + 1)));
  100.             SP := SP + 2;
  101.  
  102.          --  Upper bit shift, EUC
  103.  
  104.          elsif EM = WCEM_EUC then
  105.             R (RP) := EUC_To_JIS (S (SP), S (SP + 1));
  106.             SP := SP + 2;
  107.  
  108.          --  Upper bit shift, shift-JIS
  109.  
  110.          elsif EM = WCEM_Shift_JIS then
  111.             R (RP) := Shift_JIS_To_JIS (S (SP), S (SP + 1));
  112.             SP := SP + 2;
  113.  
  114.          --  Brackets representation
  115.  
  116.          else -- EM = WCEM_Brackets then
  117.             declare
  118.                Val : Integer;
  119.  
  120.             begin
  121.                if SP + 3 < S'Last
  122.                  and then S (SP) = '['
  123.                  and then S (SP + 1) = '"'
  124.                  and then S (SP + 2) /= '"'
  125.                then
  126.                   Val := Get_Hex (S (SP + 4)) + 16 * Get_Hex (S (SP + 3));
  127.  
  128.                   if S (SP + 5) = '"' then
  129.                      SP := SP + 6;
  130.  
  131.                   else
  132.                      Val :=
  133.                        Val * 256 +
  134.                        Get_Hex (S (SP + 6)) + 16 * Get_Hex (S (SP + 5));
  135.  
  136.                      SP := SP + 8;
  137.                   end if;
  138.  
  139.                   R (RP) := Wide_Character'Val (Val);
  140.                end if;
  141.             end;
  142.          end if;
  143.       end loop;
  144.  
  145.       return R (1 .. RP);
  146.    end String_To_Wide_String;
  147.  
  148. end System.WCh_StW;
  149.